package cn.shlbysj.demo.controller;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.NewsDiscusInfo;
import cn.shlbysj.demo.entity.NewsInfo;
import cn.shlbysj.demo.service.IArticleService;
import cn.shlbysj.demo.service.INewsDiscusInfoService;
import cn.shlbysj.demo.service.INewsInfoService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/news")
@Api("毕设资讯模块")
public class NewsInfoController {

    @Autowired
    private INewsInfoService newsInfoService;
    @Autowired
    private INewsDiscusInfoService newsDiscusInfoService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取资讯列表接口")
    @ResponseBody
    public Result list(HttpServletRequest request,
                       @ApiParam(name="title",value="标题")@RequestParam(value = "title",required = false) String title,
                       @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                       @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = newsInfoService.findPage(title,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/selectOne",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取单条资讯信息")
    @ResponseBody
    public Result selectOne(HttpServletRequest request,
                       @ApiParam(name="nId",value="资讯ID",required = true)@RequestParam(value = "nId",required = false) String nId
    ){
        try {
            NewsInfo bo= newsInfoService.getOne(nId);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(bo),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "发布资讯")
    @ResponseBody
    public Result insert(HttpServletRequest request,
                         @ApiParam(name="newsInfo",value="资讯信息")@RequestBody NewsInfo newsInfo
    ){
        try {
            return newsInfoService.insert(newsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "修改资讯")
    @ResponseBody
    public Result update(HttpServletRequest request,
                         @ApiParam(name="newsInfo",value="资讯信息")@RequestBody NewsInfo newsInfo
    ){
        try {
            return newsInfoService.update(newsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "删除资讯")
    @ResponseBody
    public Result delete(HttpServletRequest request,
                         @ApiParam(name="nId",value="主键ID",required = true)@RequestParam String nId
    ){
        try {
            return newsInfoService.delete(nId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/discuss/list",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取新闻评论列表接口")
    @ResponseBody
    public Result discussList(HttpServletRequest request,
                              @ApiParam(name="newsId",value="资讯ID")@RequestParam(value = "newsId",required = false) String newsId,
                              @ApiParam(name="page",value="当前页",required = true)@RequestParam(value = "page",required = true) Integer page ,
                              @ApiParam(name="rows",value="每页显示多少条",required = true)@RequestParam(value = "rows",required = true) Integer rows){
        try {
            PageResult pageResult = newsDiscusInfoService.findPage(newsId,page,rows);
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, JSON.toJSONString(pageResult),"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }

    }
    @RequestMapping(value = "/discuss/insert",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "发布资讯评论")
    @ResponseBody
    public Result discussInsert(HttpServletRequest request,
                         @ApiParam(name="newsDiscusInfo",value="资讯评论信息")@RequestBody NewsDiscusInfo newsDiscusInfo
    ){
        try {
            return newsDiscusInfoService.insert(newsDiscusInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/discuss/update",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "修改资讯评论")
    @ResponseBody
    public Result discussUpdate(HttpServletRequest request,
                         @ApiParam(name="newsDiscusInfo",value="资讯评论信息")@RequestBody NewsDiscusInfo newsDiscusInfo
    ){
        try {
            return newsDiscusInfoService.update(newsDiscusInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
    @RequestMapping(value = "/discuss/delete",method = RequestMethod.DELETE)
    @ApiOperation(httpMethod = "DELETE", value = "修改资讯评论")
    @ResponseBody
    public Result discussDelete(HttpServletRequest request,
                                @ApiParam(name="dId",value="评论ID")@RequestParam(value = "dId",required = false) String dId
    ){
        try {
            return newsDiscusInfoService.delete(dId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
        }
    }
}
